const express = require('express');
const router = express.Router();
const user = require('../module/user/user')
const login = require("../module/login/login");
const like = require("../module/like/like");

// 获取当前用户的信息
router.get('/userInfo', async (req, res, next) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  console.log(loginStatus)
  if (loginStatus.loginStatus) {
    const data = await user.userInfo(loginStatus?.loginRows)
    res.json(data)
  }else {
    res.json(loginStatus.msg)
  }
});

// 获取我的查看路线历史
router.get('/myHistory', async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await user.getHistoryList({
      ...loginStatus?.loginRows,
      ...req.query
    })
    res.json(data)
  }else {
    res.json(loginStatus.msg)
  }
})

// 查询我收藏的路线
router.get('/myLike', async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await like.getLikeByUserId({
      ...loginStatus?.loginRows,
      ...req.query
    })
    res.json(data)
  }else {
    res.json(loginStatus.msg)
  }
})

// 查询我收藏的路线数量
router.get('/getLikeTotalByUserId', async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await like.getLikeTotalByUserId({
      ...loginStatus?.loginRows,
      ...req.query
    })
    res.json(data)
  }else {
    res.json(loginStatus.msg)
  }
})

// 收藏路线
router.post('/addLike', async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await like.addLike({
      ...loginStatus?.loginRows,
      ...req.body
    })
    res.json(data)
  }else {
    res.json(loginStatus.msg)
  }
})

// 取消收藏
router.post('/removeLike', async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await like.removeLike({
      ...loginStatus?.loginRows,
      ...req.body
    })
    res.json(data)
  }else {
    res.json(loginStatus.msg)
  }
})

module.exports = router;
